package com.qf.config;


import com.qf.common.vo.MemberVo;
import com.qf.entity.Member;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

/**
 * @author 86155
 */
public class ShiroUtil {

    /**
     * 循环次数
     */
    public final static int HASH_ITERATIONS = 1024;

    public static String sha256(String password, String salt) {
        return new SimpleHash(Sha256Hash.ALGORITHM_NAME, password, salt, HASH_ITERATIONS).toString();
    }



    /**
     * 获取会话
     */
    public static Session getSession() {
        return SecurityUtils.getSubject().getSession();
    }

    /**
     * Subject：主体，代表了当前“用户”
     */
    public static Subject getSubject() {
        return SecurityUtils.getSubject();
    }

    public static Member getMemberInfo() {
        return (Member) SecurityUtils.getSubject().getPrincipal();
    }

    public static MemberVo getMemberVoInfo() {
        return (MemberVo) SecurityUtils.getSubject().getPrincipal();
    }
    public static Long getMemberId() {
        return getMemberInfo().getUid();
    }

    public static void setSessionAttribute(Object key, Object value) {
        getSession().setAttribute(key, value);
    }

    public static Object getSessionAttribute(Object key) {
        return getSession().getAttribute(key);
    }

    public static boolean isLogin() {
        return SecurityUtils.getSubject().getPrincipal() != null;
    }

    public static void logout() {
        SecurityUtils.getSubject().logout();
    }
}
